package com.ruoyi.web.controller.order;

import com.alipay.api.AlipayApiException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.DriverInfo;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.dto.OrderVo;
import com.ruoyi.web.utils.PayData;
import com.ruoyi.web.utils.ZhifubaoUtill;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/addons")
@CrossOrigin
public class OrderController {

    @Resource
    ZhifubaoUtill zhifubaoUtill;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IOrderDtoService iOrderDtoService;

    @Autowired
    private ITbPassengerService tbPassengerService;

    @PostMapping("/ddrive/order/create")
    public com.ruoyi.system.domain.OrderDto create(@RequestBody OrderDto orderDto){
        int i;
        HashMap<String, Object> map = new HashMap<>();
        orderDto.setCreateTime(new Date());
        orderDto.setStatus(0L);
        Random random = new Random();
        int code = random.nextInt(1000000);
        if (code<100000){
            code+=100000;
        }
        orderDto.setOrderId((long) code);
        orderDto.setStatusTest("未接单");

        iOrderDtoService.insertOrderDto(orderDto);
        TbPassenger user = tbPassengerService.selectTbPassengerById(orderDto.getUserId());
        Boolean aBoolean = redisTemplate.hasKey(orderDto.getUserId() + "");
        if (aBoolean){
            i = (int) redisTemplate.opsForValue().get(orderDto.getUserId() + "");
        }else {
            i=0;
        }
        OrderVo orderVo = OrderVo.builder().orderId(orderDto.getOrderId())
                .orderMoney(orderDto.getOrderMoney())
                .overtimePrice(orderDto.getOvertimePrice())
                .start(orderDto.getStart())
                .end(orderDto.getEnd())
                .startPoint(orderDto.getStartPoint())
                .endPoint(orderDto.getEndPoint())
                .passengerCode(user.getPassengerCode())
                .passengerName(user.getPassengerName())
                .passengerPhone(user.getPassengerPhone())
                .passengerSex(user.getPassengerSex())
                .passengerId(user.getPassengerId())
                .cancelNum(i)
                .build();
        redisTemplate.opsForValue().set("redisOrderVo",orderVo,5, TimeUnit.MINUTES);
        rabbitTemplate.convertAndSend("2010a",orderVo);
        return orderDto;
    }

    @PostMapping("/ddrive/order/send")
    public OrderDto send(@RequestBody OrderDto order){
        OrderDto orderDto = iOrderDtoService.selectOrderDtoByOrderId(order.getOrderId());
        return orderDto;
    }


    @PostMapping("/ddrive/order/cancel")
    public void cancel(@RequestBody OrderDto orderDto){
        orderDto.setStatus((long) -1);

        iOrderDtoService.updateOrderDto(orderDto);

        Boolean aBoolean = redisTemplate.hasKey(orderDto.getUserId() + "");
        if (aBoolean){
            int i= (int) redisTemplate.opsForValue().get(orderDto.getUserId() + "");
            redisTemplate.opsForValue().set(orderDto.getUserId()+"",i+1);
        }else {
            redisTemplate.opsForValue().set(orderDto.getUserId()+"",1);
        }

    }


    @GetMapping("/ddrive/order/updateByDriver")
    public AjaxResult updateByDriver(@RequestParam("orderId") Long orderId, @RequestParam("driverCode") String driverCode){

        OrderDto orderDto = iOrderDtoService.selectOrderDtoByOrderId(orderId);
        orderDto.setStatusTest("已接单");
        orderDto.setStatus(1L);
        orderDto.setDriverCode(driverCode);

        iOrderDtoService.updateOrderDto(orderDto);

        return AjaxResult.success();
    }

    @GetMapping("/ddrive/order/uploadByOrderId")
    public AjaxResult uploadByOrderId(@RequestParam("orderId") Long orderId,@RequestParam("statusTest")String statusTest,@RequestParam("status")Long status){

        OrderDto orderDto = iOrderDtoService.selectOrderDtoByOrderId(orderId);
        orderDto.setStatusTest(statusTest);
        orderDto.setStatus(status);
        iOrderDtoService.updateOrderDto(orderDto);
        return AjaxResult.success();
    }


    @GetMapping("/ddrive/order/getOrderStatus")
    public OrderDto getOrderStatus(@RequestParam("orderId") Long orderId){
        OrderDto orderDto = iOrderDtoService.selectOrderByOrderId(orderId);
        return orderDto;
    }



    @RequestMapping("/zfb")
    public String zfb(@RequestParam("code")String code, @RequestParam("money")Integer money , @RequestParam("name")String name, HttpServletResponse response) throws AlipayApiException {

        PayData payData=new PayData();
        payData.setCode(code);
        payData.setMoney(money);
        payData.setName(name);

        Map pay = zhifubaoUtill.pay(payData);
        String form = (String) pay.get("form");
        return form;

    }


    @GetMapping("/ddrive/order/talkingList")
    public AjaxResult talkingList(@RequestParam("userId")Long userId){
        OrderDto orderDto=new OrderDto();
        orderDto.setUserId(userId);
        List<OrderDto> orderDtos = iOrderDtoService.selectOrderDtoList(orderDto);

        return AjaxResult.success(orderDtos);
    }
    @Autowired
    private DriverService driverService;

    @Autowired
    private PurseService purseService;

    @GetMapping("/ddrive/order/uploadDriverNum")
    public AjaxResult uploadDriverNum(@RequestParam("driverID")String driverID ,@RequestParam("driverNum")Integer driverNum){
        Driver driver= driverService.selectByDriverId(driverID);
        Integer num=driverNum+1;
        if (num==100){
            String driverCode = driver.getDriverCode();
            Purse purse = purseService.selectByDriverCode(driverCode);
            purse.setCash(purse.getCash()+300);
            purseService.updatePurse(purse);

        }
        if (num==300){
            String driverCode = driver.getDriverCode();
            Purse purse = purseService.selectByDriverCode(driverCode);
            purse.setCash(purse.getCash()+500);
            purseService.updatePurse(purse);
        }


        driverService.uploadDriverNum(driverID,num);

        return AjaxResult.success();
    }

    @Autowired
    private ITbMemberService iTbMemberService;


    @GetMapping("/ddrive/order/selectByMember")
    public AjaxResult selectByMember(@RequestParam("userCode")String userCode){
            List<TbMember> tbMembers= iTbMemberService.selectByMember(userCode);
        TbMember tbMember = tbMembers.get(0);
        return AjaxResult.success(tbMember);
    }

    @GetMapping("/ddrive/order/delBy")
    public AjaxResult delBy(@RequestParam("userCode")String userCode,@RequestParam("couponNum")Integer couponNum){
        iTbMemberService.updateBy(userCode,couponNum);
        return AjaxResult.success();
    }






}

